package interview.cmbbank;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;


/**
 * description: 2023-2-26 招商银行笔试题
 *
 * @author <a href="zhaoyingling12@163.com">Simon</a>
 * @date 2023/2/26
 */
public class Test1 {

    public static void main(String[] args) {
        Solution solution = new Solution();
        ArrayList times = new ArrayList();
//        times.add("12:30:10");
//        times.add("12:15:12");
//        times.add("15:20:14");
        // 输入 ["00:00:00","00:10:01","00:02:00","00:00:03"]
        // 输出 ["00:00:00","00:02:00","00:10:01","00:00:03"]
        times.add("00:00:00");
        times.add("00:10:01");
        times.add("00:02:00");
        times.add("00:00:03");

        System.out.println(solution.timeSort(times));

    }


    public static class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
         *
         * @param times string字符串ArrayList
         * @return string字符串ArrayList
         */
        public ArrayList<String> timeSort(ArrayList<String> times) {
            // write code here
            TimeDTO[] timeDTOArray = new TimeDTO[times.size()];
            int index = 0;
            for (String time : times) {
                timeDTOArray[index++] = new TimeDTO(time);
            }

            Comparator comparator = new Comparator<TimeDTO>() {
                @Override
                public int compare(TimeDTO o1, TimeDTO o2) {
                    // ["12:30:10","12:15:12","15:20:14"]
                    if (o1.getSeconds() < o2.getSeconds()) {
                        return -1;
                    }
                    if (o1.getSeconds() > o2.getSeconds()) {
                        return 1;
                    }
                    // 秒数相同看分
                    if (o1.getMinutes() < o2.getMinutes()) {
                        return -1;
                    }
                    if (o1.getMinutes() > o2.getMinutes()) {
                        return 1;
                    }
                    // 分相同看小时
                    return o1.getHour() < o2.getHour() ? -1 : o1.getHour() == o2.getHour() ? 0 : 1;
                }
            };
            Arrays.sort(timeDTOArray, comparator);

            ArrayList<String> result = new ArrayList<>();
            for (TimeDTO timeDTO : timeDTOArray) {
                result.add(timeDTO.toString());
            }
            return result;
        }

        class TimeDTO {

            private int seconds;

            private int minutes;

            private int hour;

            public TimeDTO(String time) {
                String[] times = time.split(":");
                this.seconds = Integer.valueOf(times[2]);
                this.minutes = Integer.valueOf(times[1]);
                this.hour = Integer.valueOf(times[0]);
            }

            public String toString() {
                String strHour = hour <= 9 ? "0" + hour : hour + "";
                String strMinutes = minutes <= 9 ? "0" + minutes : minutes + "";
                String strSeconds = seconds <= 9 ? "0" + seconds : seconds + "";
                return strHour + ":" + strMinutes + ":" + strSeconds;
            }

            public Integer getSeconds() {
                return seconds;
            }

            public void setSeconds(Integer seconds) {
                this.seconds = seconds;
            }

            public Integer getMinutes() {
                return minutes;
            }

            public void setMinutes(Integer minutes) {
                this.minutes = minutes;
            }

            public Integer getHour() {
                return hour;
            }

            public void setHour(Integer hour) {
                this.hour = hour;
            }
        }
    }

}
